﻿using System;
using System.Collections.Generic;
using PEProtocol;

/**
 * 聊天业务模块
 */

public class ChatSys{
    private static ChatSys instance = null;

    public static ChatSys Instance{
        get{
            if (instance == null){
                instance = new ChatSys();
            }

            return instance;
        }
    }

    private CacheSvc cacheSvc;

    public void Init(){
        cacheSvc = CacheSvc.Instance;
        PECommon.Log("ChatSvc Init Done");
    }

    public void SndChat(MsgPack pkt){
        SndChat data = pkt.msg.sndChat;
        PlayerData pd = cacheSvc.GetPlayerDataBySession(pkt.session);

        //任务进度数据更新
        TaskSys.Instance.CalcTaskPrgs(pd, 6);
        
        GameMsg msg = new GameMsg{
            cmd = (int)CMD.PshChat,
            pshChat = new PshChat{
                name = pd.name,
                chat = data.chat
            }
        };
        byte[] bytes = PENet.PETool.PackNetMsg(msg);
        //广播给所有在线客户端
        List<ServerSession> onlineServerSessions = cacheSvc.GetOnlineServerSessions();
        for (int i = 0; i < onlineServerSessions.Count; i++){
            onlineServerSessions[i].SendMsg(bytes);
        }
    }
}
